package com.sinping.qh.service.auth.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sinping.qh.domain.auth.Token;
import com.sinping.qh.domain.auth.User;
import com.sinping.qh.repository.mysql.dao.TokenMapper;
import com.sinping.qh.service.auth.ITokenService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sinping.qh.utils.misc.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨云博
 * @since 2017-12-21
 */
@Service
public class TokenServiceImpl extends ServiceImpl<TokenMapper, Token> implements ITokenService {

    @Autowired
    private ITokenService iTokenService;

    @Override
    public String getUserToken(User user) {
        EntityWrapper<Token> tokenEntityWrapper = new EntityWrapper<Token>();
        Token tokenSelect = new Token();
        tokenSelect.setUserId(user.getId());
        tokenEntityWrapper.setEntity(tokenSelect);
        Token get = iTokenService.selectOne(tokenEntityWrapper);
        if (get != null) {
            return get.getToken();
        } else {
            return IdGenerator.uuid2();
        }
    }

    @Override
    public Token getTokenByUserId(String user_id) {
        EntityWrapper<Token> tokenEntityWrapper = new EntityWrapper<Token>();
        Token tokenSelect = new Token();
        tokenSelect.setUserId(user_id);
        tokenEntityWrapper.setEntity(tokenSelect);
        Token token = null;
        try {
            token = this.selectOne(tokenEntityWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            token = null;
        }
        return token;
    }
}
